<?php

class menuEdit extends bee\form
{ protected static $__formID = "frm201109060224";
  public $__caption = "Menu editor";
  public $__version = "1.0";

  public $tree;
  public $meni;
  public $yn;
  public $parents;
  protected $types;

  protected $helper;

  protected $id;
  protected $name;
  protected $description;
  protected $command;
  protected $ord_nr;
  protected $active;
  protected $parent;
  protected $type;

  protected $novi;
  protected $brisi;
  protected $uredi;

  public function createForm($ect)
  {  $this->yn          = bee\getListByRange(1);
     $this->types       = bee\getListByRange(16);
     $wm = new bee\webMeni();
     $rs_wm = new bee\rowSet($wm);
     $this->parents  = $rs_wm->getList("name","id");
     $this->meni = new bee\webMeni();

     $this->id             = new bee\text("id",           $this->meni->id);
     $this->name           = new bee\text("name",         $this->meni->name);
     $this->description    = new bee\text("description",  $this->meni->description);
     $this->command        = new bee\text("command",      $this->meni->command);
     $this->command->autoComplete = new bee\autoComplete();
     $this->command->autoComplete->source = array("abc", "ace", "gea", "dean");
     //$this->command->autoComplete->source = "dzejson";
     $this->command->autoComplete->minLength = 2;
     $this->ord_nr         = new bee\text("ord_nr",       $this->meni->ord_nr);
     $this->active         = new bee\option("active",     $this->meni->active, $this->yn );// bee\text("active", $wm->active);
     $this->parent         = new bee\option("parent",     $this->meni->parent, $this->parents);
     $this->parent->notSelectedValue = "";
     $this->parent->notSelectedText = "TOP";
     $this->type           = new bee\option("type",       $this->meni->type, $this->types); //bee\text("type", $wm->type);

     //$RCommand = new bee\webMeni();
     //$this->helper = new textHelper($RCommand, "command");
     //$this->command->setHelper($this->helper);

     $this->novi = new bee\link("novi", "");
     $this->novi->onClickFn = new bee\eventDetails(ACTION_TYPE_AJAX, "dodaj", "","#main");

     $this->brisi = new bee\button("brisi", "delete");
     $this->brisi->onClickFn = new bee\eventDetails(ACTION_TYPE_POST_AJAX_FORM, "delete", "#main");

     $this->uredi = new bee\button("pohrani", "save");
     $this->uredi->onClickFn = new bee\eventDetails(ACTION_TYPE_POST_AJAX_FORM, "uredi", "#main");


  }

  public function loadForm($evt)
  { $this->meni->SELECT(isset($evt["id"]) ? $evt["id"] : -1);
  }

  public function drawForm($evt)
  { $meni = new bee\webMeni();
    $this->tree = new bee\tree(clone $meni);
    $this->attachChildren($this->tree);
    $this->tree->key = -1;

    static::$DSDoc->setObject("/form/others/o1", $this->tree);
    static::$DSDoc->setRow($this->meni, "menis");
  }

  private function attachChildren(bee\tree $top)
  { static $level=0;
    $meni = new bee\webMeni();
    $meni2 = new bee\webMeni();
    $meni->parent = $top->value->id;

    if($top->value->id)
    { $aw = "";
    } else
    {  $aw = " AND (parent IS NULL OR parent = 0)";
    }
    $rs = new bee\rowSet(clone $meni, new bee\afterWhere($aw . " ORDER BY ord_nr "),0,0);

    while($rs->getRow($meni2))
    { $novi = $top->addChild(clone $meni2, $meni2->id);
      $l=  new bee\link("lnk_" . $meni2->id, $meni2->name);
      $l->onClickFn = new bee\eventDetails(ACTION_TYPE_AJAX, "uredi", "#main");
      $this->links[$meni2->id] = $l;
      $this->attachChildren($novi);
    }
    return $top;
  }

  public function uredi($evt)
  { $this->meni->fillFromArray($evt);
    if($this->meni->id)
    { $this->meni->UPDATE();
    } else
    { $this->meni->INSERT();
    }
  }

  public function dodaj($evt)
  { $this->meni->SELECT(-1);
  }

  public function delete($evt)
  {  $this->meni->DELETE();
  }

  public function dzejson($evt)
  {//die(print_r($evt,true));
    $data = array("ante", "mate", "jure", "slavko", "ivo", "miro");
    $farr = array();
    foreach ($data as $value)
    { if(stristr($value, $evt["term"]))
      { $farr[] = $value;
      }
    }
    die(json_encode($farr));
  }
}
